package com.inkyi.assistant.app.tools.comps;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inkyi.assistant.app.tools.base.InkTools;
import com.inkyi.framework.common.utils.JsonUtils;
import com.inkyi.framework.daqian.share.layout.HorizontalLayout;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class JsonViewTool extends JPanel implements InkTools {

    private JSplitPane workPanel = null;
    private JPanel headerPanel = null;

    private JTextArea inputField = null;
    private JScrollPane inputScroll = null;

    private RSyntaxTextArea outputField = null;
    private RTextScrollPane outputScroll = null;

    private JButton formatButton = null;
    private JButton clearButton = null;

    public JsonViewTool() {
        initComponents();
    }

    @Override
    public void initComponents() {
        this.setLayout(new BorderLayout());
        // 工作区
        workPanel = new JSplitPane();

        inputField = new JTextArea();
        inputField.setLineWrap(true);
        inputScroll = new JScrollPane(inputField);

        outputField = new RSyntaxTextArea();
        outputField.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
        outputField.setHighlightCurrentLine(true);
        outputField.setLineWrap(true);
        outputField.setEditable(false);
        outputField.setCodeFoldingEnabled(true);
        outputScroll = new RTextScrollPane(outputField);
        outputScroll.setLineNumbersEnabled(true);

        workPanel.setLeftComponent(inputScroll);
        workPanel.setRightComponent(outputScroll);

        // 头部
        headerPanel = new JPanel();
        headerPanel.setLayout(new HorizontalLayout());
        headerPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        formatButton = new JButton();
        formatButton.setText("格式化");

        clearButton = new JButton();
        clearButton.setText("清空");

        headerPanel.add(formatButton);
        headerPanel.add(Box.createHorizontalStrut(10));
        headerPanel.add(clearButton);


        this.add(headerPanel, BorderLayout.NORTH);
        this.add(workPanel, BorderLayout.CENTER);
        // 按钮处理
        formatButton.addActionListener(e -> {
            try {
                String text = inputField.getText();
                if (text == null || text.isEmpty()) {
                    outputField.setText("");
                } else {
                    JsonNode obj = JsonUtils.parseJSONObject(text);
                    ObjectMapper mapper = JsonUtils.getObjectMapper();
                    DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("  ", DefaultIndenter.SYS_LF);
                    DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
                    printer.indentObjectsWith(indenter); // 缩进 JSON 对象
                    printer.indentArraysWith(indenter);  // 缩进 JSON 数组
                    String format = mapper.writer(printer).writeValueAsString(obj);
                    if (format == null || format.isEmpty() || format.equals("null")) {
                        outputField.setText("格式不正确");
                    } else {
                        outputField.setText(format);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        clearButton.addActionListener(e -> {
            inputField.setText("");
            outputField.setText("");
        });

        workPanel.setDividerLocation(0.5);
    }

    @Override
    public String getToolCode() {
        return "json-view-format";
    }

    @Override
    public String getToolName() {
        return "JSON格式化";
    }

    @Override
    public String getToolID() {
        return "tools-" + getToolCode();
    }

    @Override
    public JComponent getComponent() {
        return this;
    }
    @Override
    public String getOpenType() {
        return "workspace";
    }

    public static void main(String[] args) throws JsonProcessingException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        JsonViewTool panel = new JsonViewTool();
        panel.initComponents();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }


}
